EFFECT.clipplanes = {}
EFFECT.impactplanes = {}

function EFFECT:Init(data)
	self.Entity:SetParent(data:GetEntity())
	self.heirach = data:GetEntity()
	if self.heirach == NULL then return end
	self.mat = Material(self.heirach:GetNetworkedString("impactmaterial"))
	--self.mat = Material("models/shadertest/shader3")
	self.Entity:SetModel("models/zup/shields/200_shield.mdl")
	self.Radius = self.heirach:GetNetworkedInt("Radius") * 1.01
	self.Entity:SetColor(255,255,255,70)
	local ModelSize = (self.Entity:OBBMaxs() - self.Entity:OBBCenter()).z
	self.ModelScale = self.Radius/ModelSize
	self.Entity:SetPos((data:GetEntity():GetPos() - self.Entity:LocalToWorld(self.Entity:OBBCenter() * self.ModelScale)) + data:GetEntity():GetPos())
	
	local boundlimit = self.Radius*Vector()
	self.Entity:SetRenderBounds(-2*boundlimit,2*boundlimit)
	
	self.StartTime = CurTime()
	self.EndTime = CurTime() + 1
	self.LifeTime = 1
	
	self.Normal = data:GetNormal()
	
	self.Scale = 0.2 --data:GetScale()
	
	self.color = self.heirach:GetNetworkedVector( "impactcolor" )
	
	self.Alpha = 1
	
	self.clipplanes = {}
	self.impactplanes = {}
end

function EFFECT:Think()
	if self.heirach:IsValid() then
		self.clipplanes = self.heirach.ClipPlanes
	else
		self.clipplanes = {}
	end
	if ((self.Alpha or 0) <= 0) then
		return false
	else
		self.Alpha = math.Clamp(1 - ((CurTime() - self.StartTime) / self.LifeTime), 0, 1)
		self.Entity:SetColor(self.color.x, self.color.y, self.color.z, self.Alpha * 255)
		local enddistance, extradistance = (1 - self.Scale) * self.Radius, self.Alpha * self.Scale * self.Radius
		self.impactplanes = {Plane(self.Normal * -1, self.Entity:LocalToWorld(self.Entity:OBBCenter()) + ((self.Normal * -1) * (enddistance + extradistance))), Plane(self.Normal, self.Entity:LocalToWorld(self.Entity:OBBCenter()) + ((self.Normal * -1) * (enddistance + (extradistance * 1.1))))}
		return true
	end
end 

function EFFECT:Render()
		local size = self.ModelScale
		local clip = true --util.tobool(GetConVar("cl_cdamage_shieldclippingenabled"))
		SetMaterialOverride(self.mat)
		self.Entity:SetModelScale(Vector()*size)
		local planes = 0
		if (clip) then
			render.EnableClipping(true)
			if self.clipplanes then
				for _,plane in pairs(self.clipplanes) do
					render.PushCustomClipPlane( plane.Normal, plane.Distance)
					planes = planes + 1
				end
			end
			for _,plane in pairs(self.impactplanes) do
				render.PushCustomClipPlane( plane.Normal, plane.Distance)
				planes = planes + 1
			end
		end
		if (util.tobool(#self.impactplanes)) then
			self.Entity:DrawModel()
		end
		for remover=0, planes do
			render.PopCustomClipPlane()
		end
		render.EnableClipping(false)
		SetMaterialOverride(nil)
end